# PowerShell 依赖安装脚本
# 使用方法: .\install-deps.ps1

param(
    [switch]$Dev = $false,
    [switch]$Force = $false
)

# 检查是否在虚拟环境中
if (-not $env:VIRTUAL_ENV) {
    Write-Host "⚠️  不在虚拟环境中，正在自动激活..." -ForegroundColor Yellow
    .\activate.ps1
    if (-not $env:VIRTUAL_ENV) {
        Write-Host "❌ 无法激活虚拟环境" -ForegroundColor Red
        return
    }
}

# 检查Python是否可用
$pythonPath = Get-Command "python" -ErrorAction SilentlyContinue
if (-not $pythonPath) {
    Write-Host "❌ 找不到Python命令，请检查虚拟环境设置" -ForegroundColor Red
    return
}

# 检查pip是否可用
$pipVersion = & python -m pip --version 2>$null
if (-not $pipVersion) {
    Write-Host "❌ 找不到pip命令，虚拟环境可能损坏" -ForegroundColor Red
    return
}

# 升级pip
Write-Host "📦 升级pip..." -ForegroundColor Cyan
& python -m pip install --upgrade pip

# 安装基础依赖
Write-Host "📦 安装基础依赖..." -ForegroundColor Cyan
& pip install -r requirements.txt

if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ 基础依赖安装失败" -ForegroundColor Red
    return
}

# 安装开发依赖
if ($Dev) {
    Write-Host "📦 安装开发依赖..." -ForegroundColor Cyan
    & pip install -r requirements-dev.txt
    
    if ($LASTEXITCODE -ne 0) {
        Write-Host "⚠️  开发依赖安装失败，但基础依赖已安装" -ForegroundColor Yellow
    }
} else {
    $installDev = Read-Host "是否安装开发依赖? (y/N)"
    if ($installDev -eq "y" -or $installDev -eq "Y") {
        Write-Host "📦 安装开发依赖..." -ForegroundColor Cyan
        & pip install -r requirements-dev.txt
        
        if ($LASTEXITCODE -ne 0) {
            Write-Host "⚠️  开发依赖安装失败，但基础依赖已安装" -ForegroundColor Yellow
        }
    }
}

# 验证安装
Write-Host "🔍 验证安装结果..." -ForegroundColor Cyan
try {
    & python -c "import fastapi, sqlalchemy, pydantic; print('✅ 核心依赖验证成功')"
} catch {
    Write-Host "❌ 依赖验证失败: $_" -ForegroundColor Red
}

# 显示安装信息
Write-Host ""
Write-Host "================================" -ForegroundColor Green
Write-Host "✅ 依赖安装完成！" -ForegroundColor Green
Write-Host "================================" -ForegroundColor Green
Write-Host "核心依赖:" -ForegroundColor Cyan
& pip list | Select-String "fastapi|sqlalchemy|pydantic"

Write-Host ""
Write-Host "开始开发:" -ForegroundColor Yellow
Write-Host "  启动后端: python run.py" -ForegroundColor White
Write-Host "  激活虚拟环境: .\activate.ps1" -ForegroundColor White
Write-Host "  停用虚拟环境: .\deactivate.bat" -ForegroundColor White
Write-Host ""

# 检查系统状态
Write-Host "📊 系统状态检查:" -ForegroundColor Cyan
& python -c "
import sys
print(f'Python版本: {sys.version}')
import platform
print(f'操作系统: {platform.platform()}')
import os
print(f'虚拟环境: {os.environ.get(\"VIRTUAL_ENV\", \"未激活\")}')
"